from PyQt4.QtGui import QGraphicsObject,QPen,QBrush,QColor
from PyQt4 import QtCore,QtGui,Qt
from KCharts.KGraphics.KGraphicsSceneSubViewModel import KGraphicsSceneSubViewModel
from KCharts.KGraphics.KBaseDisplayItem import KBaseDisplayItem,KBaseDisplayItemModel
from KApp import KConstants as vm


class KVolItemModel(KBaseDisplayItemModel):
	
	def GetLogicRange(self):
		(xdatarange,ydatarange)=self.m_stock.CurWindowRange('Volumn')	
		return (xdatarange,ydatarange)
	
class KVolDisplayItem(KBaseDisplayItem):
	

		
	def PaintCurves(self,painter):
		## pre-computing a QPicture object allows paint() to run much more quickly, 
		## rather than re-drawing the shapes every time.
		priceblocks=self.m_stock.CurWindowData('Price')
		XCordinates=self.m_stock.CurWindowData('Index')
		viewrect=self.viewmodel.GetViewScreenRect(vm.RectType.ContentBound)
		width=1.0/3.0
		for index in range(0,len(priceblocks)):
			(openp,highp,lowp,closep,vol)=priceblocks[index]
			xcord=XCordinates[index]
			# Convert coordinate to scene coordinate
			([xcord],[vol])=self.viewmodel.mapLogiceCordToScene(vm.RectType.ContentBound,[xcord],[vol])
			if openp < closep:
				painter.setPen(QPen(QColor(0,255,0)))			
				
			else:
				painter.setPen(QPen(QColor(255,0,0)))			
			if vol>0:	# draw only when vol not empty
				painter.drawLine(QtCore.QPointF(xcord, viewrect.bottom()), QtCore.QPointF(xcord, vol))		

		
	def PrepareRenderPicture(self):
		
		self.picture = QtGui.QPicture()
		painter = QtGui.QPainter(self.picture)	
		self.PaintGrid(painter,vm.RectType.ContentBound,[0,3])
		self.PaintBorders(painter,vm.RectType.OuterBound)
		self.PaintBorders(painter,vm.RectType.InnerBound)
		self.PaintBorders(painter,vm.RectType.ContentBound)
		
		self.PaintCurves(painter)
		self.PaintXAxis(painter,vm.RectType.XAxisBottom,10)
		#self.PaintYAxis(painter,vm.RectType.YAxisRight,5)	
		
		painter.end()	


		
